package com.fdsc.erp.weixin;

import com.fdsc.erp.weixin.Message.MessageUtil;
import com.fdsc.erp.weixin.Message.resp.Image;
import com.fdsc.erp.weixin.Message.resp.ImageMessage;
import com.fdsc.erp.weixin.Message.resp.TextMessage;
import com.fdsc.erp.weixin.Utils.CommonUtil;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @Description:核心服务类
 * @Author:lishanhui
 * @Date:Create in 9:50  2018-06-06
 **/
public class CoreService {
    /**
     *@Date:Create in 9:51 2018-06-06
     *@Description:处理微信发来的请求
     *@params: * @param request
     *@return:xml
     **/
    public static String processRequest(HttpServletRequest request){
        //xml格式的消息数据
        String respxml = null;
        //默认返回的文本消息内容
        String respContent = "未知的消息类型";

        try {
            //调用parseXml方法解析请求消息
            Map<String,String> requestMap = MessageUtil.parseXml(request);
            //开发者微信号
             String toUserName = requestMap.get("ToUserName");
            //发送方账号 （openID）
             String fromUserName = requestMap.get("FromUserName");
            //消息类型（text/image/location/link/voice）
             String msgType = requestMap.get("MsgType");

            //回复文本消息
            TextMessage textMessage = new TextMessage();
            textMessage.setToUserName(fromUserName);
            textMessage.setFromUserName(toUserName);
            textMessage.setCreateTime(new Date().getTime());
            textMessage.setMsgType(MessageUtil.REQP_MESSAGE_TYPE_TEXT);

            //文本消息
           /* if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)){
                //我 接收的消息
                String content = requestMap.get("Content");
                respContent = "您发送的是文本消息,内容是"+content;
            }
            //图片消息
            else  if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)){
                respContent = "您发送的是图片消息";
                ImageMessage imageMessage = new ImageMessage();
                imageMessage.setFromUserName(toUserName);
                imageMessage.setToUserName(fromUserName);
                imageMessage.setCreateTime(new Date().getTime());
                imageMessage.setMsgType(MessageUtil.REQP_MESSAGE_TYPE_IMAGE);
                Image image = new Image();
                image.setMediaId(requestMap.get("MediaId"));
                imageMessage.setImage(image);
                respxml = MessageUtil.messageToXml(imageMessage);
                return respxml;
            }
            //语音消息
            else  if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)){
                //语音消息文件标识
                String mediaId = requestMap.get("MediaId");
                //语音格式:amr
                String format  = requestMap.get("Format");
                //识别语音结果
                String recognition = requestMap.get("Recognition");
                respContent = "您发送的是语音消息:"+recognition;
            }
            //视频消息
            else  if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VIDEO)){
                respContent = "您发送的是视频消息";
            }
            //地理位置消息
            else  if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)){
                //纬度
                String latitude = requestMap.get("Location_X");
                //经度
                String longitude = requestMap.get("Location_Y");
                //地理位置
                String Label = requestMap.get("Label");
                respContent = "您发送的是地理位置消息,纬度:"+latitude+"\n经度:"+longitude+"\n地理位置"+Label;
            }
            //链接消息
            else  if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)){
                respContent = "您发送的是链接消息";
            }
            //事件推送
            else */ if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)){
                //事件类型
                String eventType = requestMap.get("Event");
                //关注
                if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)){
                        respContent = "感谢关注杭州共赢会，各位同学在公众号下方可以自助操作。\n" +
                                "\n" +
                                "共赢会宗旨：\n" +
                                "        个人成长、企业精进，\n" +
                                "        爱心公益、资源整合。\n" +
                                "\n" +
                                "共赢会愿景：\n" +
                                "        打造学习型组织，\n" +
                                "        成就人生梦想。\n" +
                                " \n" +
                                "共赢会使命：\n" +
                                "        创造信任、共赢的社会新生态。\n" +
                                "\n" +
                                "共赢会价值观：\n" +
                                "        创新进取、友爱互助、\n" +
                                "        正心正念、知行合一。";
                }
                //取消关注
                else  if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)){
                   //TODO 取消订阅后,用户不会在收到公众账号发送的消息，因此不需要回复
                }
                //扫描带参数二维码
               /* else  if (eventType.equals(MessageUtil.EVENT_TYPE_SCAN)){
                    //处理扫描带参数二维码事件
                    respContent = "扫描了带参数的二维码,二维码id为"+requestMap.get("EventKey");
                    String ticket = requestMap.get("Ticket");
                }
                //上报地理位置
                else  if (eventType.equals(MessageUtil.EVENT_TYPE_LOCATION)){
                    //处理上报地理位置事件
                    //纬度
                    String latitude = requestMap.get("Latitude");
                    //经度
                    String longitude = requestMap.get("Longitude");
                    respContent = "纬度:"+latitude+"\n经度:"+longitude;
                }
                //自定义菜单
                else  if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)){
                    //事件KEY值，与创建菜单时的key值对应
                    String eventKey= requestMap.get("EventKey");
                    if (eventKey.equalsIgnoreCase("V1001_0")) {
                        //开锁
                    }
                }*/
            }
            //设置文本消息的内容
            textMessage.setContent(respContent);
            //将文本消息对象转换成xml
            respxml = MessageUtil.messageToXml(textMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return respxml;
    }
}
